<template>
  <div
    v-if="modelValue"
    class="modal-overlay"
    @click="closeOnBackdrop && $emit('update:modelValue', false)"
  >
    <div class="modal-container" @click.stop>
      <div class="modal-header">
        <h3 class="modal-title">{{ title }}</h3>
        <button class="modal-close" @click="$emit('update:modelValue', false)">
          <div class="i-carbon-close"></div>
        </button>
      </div>
      <div class="modal-body">
        <slot></slot>
      </div>
      <div class="modal-footer" v-if="$slots.footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  defineProps({
    modelValue: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      default: '对话框'
    },
    closeOnBackdrop: {
      type: Boolean,
      default: true
    }
  })

  defineEmits(['update:modelValue'])
</script>

<style lang="scss" scoped>
  .modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background-color: rgb(0 0 0 / 50%);
  }

  .modal-container {
    width: 100%;
    max-width: 500px;
    overflow: hidden;
    background-color: white;
    border-radius: 0.5rem;
    box-shadow:
      0 20px 25px -5px rgb(0 0 0 / 10%),
      0 10px 10px -5px rgb(0 0 0 / 4%);
  }

  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #e5e7eb;
  }

  .modal-title {
    margin: 0;
    font-size: 1.125rem;
    font-weight: 500;
    color: #1f2937;
  }

  .modal-close {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.25rem;
    font-size: 1.25rem;
    color: #6b7280;
    cursor: pointer;
    background: none;
    border: none;
    border-radius: 9999px;

    &:hover {
      background-color: #f3f4f6;
    }
  }

  .modal-body {
    padding: 1.5rem;
  }

  .modal-footer {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    justify-content: flex-end;
    padding: 1rem 1.5rem;
    border-top: 1px solid #e5e7eb;
  }
</style>
